﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using He.Web.UI;
using He.Common;

namespace He.Web.manage.weixin
{
    public partial class keyword_edit : Manage
    {
        public string action = Enums.ActionEnum.Add.ToString(); //操作类型
        public int id;
        public Model.wx_keyword model;
        protected void Page_Load(object sender, EventArgs e)
        {
            string _action = GetString("action");
            if (!string.IsNullOrEmpty(_action) && _action == Enums.ActionEnum.Edit.ToString())
            {
                this.action = Enums.ActionEnum.Edit.ToString();//修改类型
                if (!int.TryParse(Request.QueryString["id"] as string, out this.id))
                {
                    ShowParamError();
                    return;
                }
            }

            if (!Page.IsPostBack)
            {
                if (action == Enums.ActionEnum.Edit.ToString())
                {
                    chkLoginLevel("editWxkeyword");
                    ShowInfo();
                }
                else
                {
                    chkLoginLevel("addWxkeyword");
                    ddlMsgType.Enabled = true;
                }
            }
        }

        #region 赋值操作=================================
        private void ShowInfo()
        { 
            BLL.wx_keyword bll = new BLL.wx_keyword();
            BLL.article abll = new BLL.article();
            model = bll.GetModel(this.id);
            txtKeyword.Text = model.keyword;
            ddlMsgType.SelectedValue = model.msg_type;
            txtAnswer.Text = model.answer;
            txtLinkUrl.Text = model.link_url;
            txtSortId.Text = model.sort_id.ToString();
            ddlMsgTypeStatus();
            hidArticleIds.Value = model.article_id;
            //litArticles.Text = GetWxAnswer(this.id);
        }
        #endregion

        #region 增加操作=================================
        private bool DoAdd()
        {
            BLL.wx_keyword bll = new BLL.wx_keyword();
            Model.wx_keyword model = new Model.wx_keyword();
            model.keyword = txtKeyword.Text.Trim();
            model.answer = txtAnswer.Text;
            model.link_url = txtLinkUrl.Text.Trim();
            model.sort_id = int.Parse(txtSortId.Text.Trim());
            model.msg_type = ddlMsgType.SelectedValue;
            model.article_id = hidArticleIds.Value;

            if (bll.Add(model)>0)
            {
                SaveLogs(string.Format("[微信公众号管理]增加关键词:{0}",model.keyword));
                return true;
            }
            return false;

        }
        #endregion

        #region 修改操作=================================
        private bool DoEdit(int _id)
        {
            BLL.wx_keyword bll = new BLL.wx_keyword();
            Model.wx_keyword model = bll.GetModel(_id);
            model.keyword = txtKeyword.Text.Trim();
            model.answer = txtAnswer.Text;
            model.link_url = txtLinkUrl.Text.Trim();
            model.sort_id = int.Parse(txtSortId.Text.Trim());
            model.article_id = hidArticleIds.Value;

            if (!bll.Update(model))
            {
                return false;
            }

            return true;
        }
        #endregion

        private void ddlMsgTypeStatus()
        {
            switch (ddlMsgType.SelectedValue)
            {
                case "text":
                    this.pnText.Visible = true;
                    this.pnNews.Visible = false;
                    break;
                case "news":
                    this.pnText.Visible = false;
                    this.pnNews.Visible = true;
                    break;
                default :
                    this.pnText.Visible = false;
                    this.pnNews.Visible = false;
                    break;
            }
        }

        protected void ddlMsgType_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddlMsgTypeStatus();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (action == Enums.ActionEnum.Edit.ToString()) //修改
            {
                if (!DoEdit(this.id))
                {
                    ShowError("保存过程中发生错误啦！");
                    return;
                }
                ShowSuccess("修改信息成功啦！", LastListUrl);
            }
            else //添加
            {
                if (!DoAdd())
                {
                    ShowError("保存过程中发生错误啦！");
                    return;
                }
                ShowSuccess("添加信息成功啦！", Request.RawUrl);
            }
        }
    }
}